import { message } from 'antd';
import * as testrepServices from '../services/testreport';


export default {
  namespace: 'testreport',
  state: {
    testreportinfo: [],
    testreportinfoDetail: [],
    projectlist: [],
    RelatedJira: [],
    bug_desc: '',
    bug_details: '',
    demand_detail: '',
  },
  reducers: { // 修改state
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *fetchTestReport({ payload: { getparams } }, { call, put }) {
      const { data } = yield call(testrepServices.getTestReport, getparams);// 接口调用
      const errorMes = data.message || '查询失败';
      if (data.status !== 200) {
        message.error(errorMes);
      }
      yield put({
        type: 'save',
        payload: {
          testreportinfo: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    *addTestReport({ payload: { reportparam, cb } }, { call, put }) {
      const { data } = yield call(testrepServices.addTestReport, reportparam);
      const errorMes = data.message || '新增失败';
      if (data.status === 200) {
        message.success('新增成功！');
        if (cb) cb();
        yield put({
          type: 'fetchTestReport',
          payload: {
            getparams: {
              report_name: '',
              project_name: '',
              fix_version: '',
              reporter: '',
              status: '',
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *savesendTestReport({ payload: { reportparam, cb } }, { call, put }) {
      const { data } = yield call(testrepServices.sendTestReport, reportparam);
      const errorMes = data.msg || '发送失败';
      if (data.status === 200) {
        message.success('发送成功！');
        if (cb) cb();
        yield put({
          type: 'fetchTestReport',
          payload: {
            getparams: {
              report_name: '',
              project_name: '',
              fix_version: '',
              reporter: '',
              status: '',
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *deleteAttac({ payload: { param, cb } }, { call, put }) {
      const { data } = yield call(testrepServices.deleteFile, param);
      const errorMes = data.message || '删除失败';
      if (data.status === 200) {
        message.success('删除成功！');
        if (cb) cb();
      } else {
        message.error(errorMes);
      }
    },
    *updateTestReport({ payload: { reportparam, cb } }, { call, put }) {
      const { data } = yield call(testrepServices.updateTestReport, reportparam);
      const errorMes = data.msg || '修改失败';
      if (data.status === 200) {
        message.success('修改成功！');
        if (cb) cb();
        yield put({
          type: 'fetchTestReport',
          payload: {
            getparams: {
              report_name: '',
              project_name: '',
              fix_version: '',
              reporter: '',
              status: '',
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *downloadTestReportAttac({ payload: { fileparams, cb } }, { call }) {
      yield call(testrepServices.DownloadFile, fileparams);
      message.success('下载成功！');
      // console.log(data);
      // const errorMes = data.message || '下载失败';
      // if (data.status === 200) {
      //   message.success('下载成功！');
      //   if (cb) cb();
      // } else {
      //   message.error(errorMes);
      // }
    },
    *fetchProjectList({ payload }, { call, put }) {
      const { data } = yield call(testrepServices.GetProjectList);// 接口调用
      yield put({
        type: 'save',
        payload: {
          projectlist: data.data.objects || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    *fetchRelatedJira({ payload: { params } }, { call, put }) {
      const { data } = yield call(testrepServices.getTestReportJira, params);// 接口调用
      yield put({
        type: 'save',
        payload: {
          RelatedJira: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/testreport') {
          dispatch({ type: 'fetchProjectList' });
        }
      });
    },
  },
};
